import pandas as pd
import requests
import xlwt
import time
import csv
import json


def baidug(addr):
    url = "http://api.map.baidu.com/geocoding/v3/?"  # 百度地图API接口
    para = {
        "address": addr,  # 传入地址参数
        "output": "json",
        "ak": "7BH756TiUHZxxYLjNoxgbscHmILzkoI3"  # 百度地图开放平台申请ak
    }
    req = requests.get(url, para)
    req = req.json()
    # print(req)
    print('-' * 30)
    m = req["result"]["location"]
    g=f"{m['lng']}"
    print(g)
    a=f"{m['lat']}"
    return g
# baidug(addr="深圳")

def baidua(addr):
    url = "http://api.map.baidu.com/geocoding/v3/?"  # 百度地图API接口
    para = {
        "address": addr,  # 传入地址参数
        "output": "json",
        "ak": "7BH756TiUHZxxYLjNoxgbscHmILzkoI3"  # 百度地图开放平台申请ak
    }
    req = requests.get(url, para)
    req = req.json()
    # print(req)
    print('-' * 30)
    m = req["result"]["location"]
    # g=f"{m['lng']}"
    a=f"{m['lat']}"
    print(a)
    return a
# baidua(addr="深圳")
df3 = pd.read_excel('beijing2.xlsx')
print(type(df3))
df3['lng'] = 'lng'
df3['lat'] = 'lat'
print(df3)
time_start = time.time()
for i in df3.values:#按行的值提取
    # b = '北京'+i[1]  #第三列的地址
    # print(b)
    # i['lng'] =(df3['title']+'北京').apply(baidug)#获取经度并写入
    i['lat'] =(df3['title']+'北京').apply(baidua)
print(df3)
#     i['lat'] =df3['title'].apply(baidua)#获取纬度并写入
#     print(i)
# df3
# df3.to_excel('beijing3.xlsx')
# # time_start = time.time()
# # df3['lng'] = df3['title'].apply(baidug)
# # df3['lat'] = df3['title'].apply(baidua)
time_end = time.time()
t = time_end - time_start
df3.to_excel('beijing3.xlsx')
print('共用时%s秒' % t)




